package ${package.ServiceImpl};

<#if genSelect>
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
<#if genPageTableType == '1'>
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
</#if>
</#if>
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
<#if genRemove>
import com.ozo.boot.common.base.model.BaseBatchDTO;
</#if>
<#if genSelect>
<#if genPageTableType == '1'>
import com.ozo.boot.common.base.model.BasePage;
import com.ozo.boot.common.base.model.PageResult;
</#if>
import com.ozo.boot.common.database.util.SortUtils;
</#if>
import ${package.Entity}.${entity};
import ${package.Mapper}.${entity}Mapper;
<#if genAdd || genEdit>
import ${package.SaveDTO}.${entity}SaveDTO;
</#if>
<#if genSelect>
import ${package.QueryDTO}.${entity}QueryDTO;
import ${package.VO}.${entity}VO;
</#if>
import ${package.Service}.I${entity}Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
<#if genSelect>

import java.util.List;
import java.util.Objects;
</#if>

/**
 * ${function} 服务实现类
 *
 * @author ${author} ${authorEmail}
 * @version ${version}
 * @date ${date}
 */
@Service
public class ${entity}ServiceImpl extends ServiceImpl<${entity}Mapper, ${entity}> implements I${entity}Service {

<#if genAdd>
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(${entity}SaveDTO saveDTO) {
        ${entity} ${entityName} = BeanUtil.copyProperties(saveDTO, ${entity}.class);
        baseMapper.insert(${entityName});
    }

</#if>
<#if genRemove>
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(BaseBatchDTO<Long> batchDTO) {
        baseMapper.deleteBatchIds(batchDTO.getIdList());
    }

</#if>
<#if genEdit>
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(${entity}SaveDTO saveDTO) {
        ${entity} ${entityName} = BeanUtil.copyProperties(saveDTO, ${entity}.class);
        baseMapper.updateById(${entityName});
    }

</#if>
<#if genSelect>
<#if genPageTableType == '1'>
    @Override
    public PageResult<${entity}VO> page(BasePage basePage, ${entity}QueryDTO queryDTO) {
        Integer pageNum = basePage.getPageNum();
        Integer pageSize = basePage.getPageSize();

        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        // 排序
        String order = basePage.getOrder();
        if (StrUtil.isBlank(order)) {
            queryWrapper.lambda().orderByDesc(${entity}::getId);
        } else {
            SortUtils.setSortCondition(queryWrapper, basePage.getProp(), order);
        }
        <#if queryFieldList?size != 0>
        // 条件
        queryWrapper.lambda()
        <#list queryFieldList as field>
            <#if field.javaType == 'String'>
                .${field.pageTableQueryType}(StrUtil.isNotBlank(queryDTO.get${strUtil.upperFirst(field.javaField)}()), ${entity}::get${strUtil.upperFirst(field.javaField)}, queryDTO.get${strUtil.upperFirst(field.javaField)}())<#if field_index == queryFieldList?size - 1>;</#if>
            <#else>
                .${field.pageTableQueryType}(Objects.nonNull(queryDTO.get${strUtil.upperFirst(field.javaField)}()), ${entity}::get${strUtil.upperFirst(field.javaField)}, queryDTO.get${strUtil.upperFirst(field.javaField)}())<#if field_index == queryFieldList?size - 1>;</#if>
            </#if>
        </#list>
        </#if>
        Page<${entity}> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);

        PageResult<${entity}VO> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setPage(page.getPages());
        pageResult.setTotal(page.getTotal());
        List<${entity}> recordList = page.getRecords();
        List<${entity}VO> recordVOList = BeanUtil.copyToList(recordList, ${entity}VO.class);
        pageResult.setResults(recordVOList);
        return pageResult;
    }

<#else>
    @Override
    public List<${entity}VO> list(${entity}QueryDTO queryDTO) {
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        // 排序
        String order = basePage.getOrder();
        if (StrUtil.isBlank(order)) {
            queryWrapper.lambda().orderByDesc(${entity}::getId);
        } else {
            SortUtils.setSortCondition(queryWrapper, basePage.getProp(), order);
        }
        <#if queryFieldList?size != 0>
        // 条件
        queryWrapper.lambda()
        <#list queryFieldList as field>
            <#if field.javaType == 'String'>
            .${field.pageTableQueryType}(StrUtil.isNotBlank(queryDTO.get${strUtil.upperFirst(field.javaField)}()), ${entity}::get${strUtil.upperFirst(field.javaField)}, queryDTO.get${strUtil.upperFirst(field.javaField)}())<#if field_index == queryFieldList?size - 1>;</#if>
            <#else>
            .${field.pageTableQueryType}(Objects.nonNull(queryDTO.get${strUtil.upperFirst(field.javaField)}()), ${entity}::get${strUtil.upperFirst(field.javaField)}, queryDTO.get${strUtil.upperFirst(field.javaField)}())<#if field_index == queryFieldList?size - 1>;</#if>
            </#if>
        </#list>
        </#if>
        List<${entity}> list = baseMapper.selectList(queryWrapper);
        return BeanUtil.copyToList(list, ${entity}VO.class);
    }

</#if>
</#if>
}
